<template>
  <div>
    <div class="centernew">
      <section>
        <p class="redb"></p>
        <router-link :to="'/kuaixunde' + '?newsId=' + id" class="bg1"
          ><span></span>{{ title }}</router-link
        >
      </section>
    </div>
  </div>
</template>

<script>
import { Kuaixun } from "@/api";
export default {
  data() {
    return {
      n: 0,
      title: "微课学堂欢迎您!",
      list: [],
      id: "",
      timer: null,
    };
  },
  created() {
    this.getList();
    this.getLastTime();
  },
  methods: {
    getList() {
      Kuaixun().then((res) => {
        this.list = res.data.data;
      });
    },
    getLastTime: function () {
      this.timer = setInterval(() => {
        this.n++;
        this.title = this.list[this.n % this.list.length].newsTitle;
        this.id = this.list[this.n % this.list.length].newsid;
      }, 3000);
    },
  },
  destroyed() {
    if (this.$route.path!=='/home') {
      //如果定时器在运行则关闭
      clearInterval(this.timer);
    }
  },
};
</script>

<style scoped lang="scss">
section {
  display: flex;
  align-items: center;
}
p {
  font-size: 3.2vw;
}
.bg1 {
  font-size: 3.2vw;
  color: #000;
  width: 70vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.redb {
  width: 1.5vw;
  height: 1vh;
  background: red;
  margin-right: 2vw;
}
</style>